Ext.onReady(function() {
			var bar = new Ext.Toolbar([{
						text : "添加新课程",
						icon : '../../resource/add.gif',
						handler : addCourse
					}, {
						text : "删除",
						icon : '../../resource/delete.png',
						handler : function() {
							Ext.MessageBox.buttonText.yes = '确定';
							Ext.MessageBox.buttonText.no = '取消';
							var selectModel = grid.getSelectionModel();
							var selectRecord = selectModel.getSelections();
							if (selectRecord.length == 0) {
								Ext.Msg.alert('', '请选择至少一项');
								return;
							}
							Ext.Msg.show({
										msg : '确定删除所选项吗？删除后将无法恢复',
										buttons : Ext.Msg.YESNO,
										fn : function(bt) {
											if (bt == 'yes') {
												deleteCourse(selectRecord);
											};
										},
										icon : Ext.MessageBox.QUESTION
									});
						}
					}

			]);
	var cb=new Ext.grid.CheckboxSelectionModel();
	var store = new Ext.data.JsonStore({
        root: 'data',
        totalProperty: 'totalcount',
        fields: [
        	'course_id','course_name','admin_name','teacher_name','create_time','student_count'
        ],
        proxy: new Ext.data.HttpProxy({
            url: 'getCourseAction'
        }),
        autoLoad:{params:{start:0,limit:50}}
    });
    var edit=new Ext.form.TextField({
               allowBlank: false
    });
	var grid=new Ext.grid.GridPanel({
		columnLines: true,
		region:'center',
		loadMask:true,
		listeners:
		{
			afteredit:updateCourse
		},
		tbar:[
			bar,
			new Ext.PagingToolbar({
				store:store,
				pageSize:50,
				displayInfo: true,
				displayMsg:'第 {0} 条到 {1} 条,一共 {2} 条',
				emptyMsg:'没有记录'
			})
		],
		stripeRows:true,
		autoScroll:true,
		applyTo:'course_grid',
		frame:true,
		sm:cb,
		store:store,
		columns:[
		new Ext.grid.RowNumberer({
			header:'行号',
			width:40
		}),
		cb,
		{header:"课程编号",width:200,dataIndex:'course_id',align:'center',sortable:true},
		{header:"课程名",width:200,dataIndex:'course_name',align:'center',sortable:true},
		{header:"创建者",width:200,dataIndex:'admin_name',align:'center',sortable:true},
		{header:"负责老师",width:200,dataIndex:'teacher_name',align:'center',sortable:true},
		{header:"创建时间",width:200,dataIndex:'create_time',align:'center',sortable:true},
		{header:"学生人数",width:200,dataIndex:'student_count',align:'center',sortable:true}
		]
	});
	var courseForm=new Ext.FormPanel({
		border:false,
		frame:true,
		labelAlign:'right',
		buttonAlign:'center',
		id:'addCourseForm',
		name:'addCourseForm',
		items:[
			{
				xtype:'textfield',
				width:150,
				name:'course_name',
				id:'course_name',
				fieldLabel:'课程名称'
			},
			{
				xtype:'textfield',
				width:150,
				name:'teacher_id',
				id:'teacher_id',
				fieldLabel:'负责老师'
		}
		],
	buttons:[
		new Ext.Button({
			text:'提交',
			handler:submitCourse
		}),
		new Ext.Button({
			text:'重置',
			handler:function(){courseForm.form.reset();}
		})
	]
	});
	new Ext.Viewport({
		layout:'border',
		items:[
			grid
		]
	});
	var win=new Ext.Window({
		plain:true,
		width:300,
		height:200,
		closeAction:'hide',
		listeners:
		{
			'beforehide':function(){store.reload();}
		},
		bodyStyle:'padding:5px',
		modal:true,
		closable:true,
		animCollapse:true,
		resizable:false,
		items:[courseForm
		]
	});
	function addCourse()
	{
		courseForm.form.reset();
		courseForm.isAdd=true;
		win.setTitle("添加课程");
		win.show();
	}
	function $value(id)
	{
		return document.getElementById(id).value;
	};
	function submitCourse()
	{
		var data=getExtForm(courseForm);
		var json=Ext.util.JSON.encode(data);
		Ext.ajaxRequest(json,'addCourseAction');
	};
	function updateCourse(e)
	{
		var data={};
		data['id']=e.record.get("course_id");
		data['field']=e.grid.getColumnModel().getDataIndex(e.column);
		data['value']=e.value;
		var json=Ext.util.JSON.encode(data);
		Ext.ajaxRequest(json,'updateCourseAction','更新成功','更新失败');

	}
	function deleteCourse(record)
	{
		
		var idArray=new Array();
		for(var i=0;i<record.length;++i)
		{
			var r=record[i];
			idArray[i]=r.get('course_id');
		}
		var json=Ext.util.JSON.encode(idArray);
		Ext.ajaxRequest(json,'deleteCourseAction');
		store.reload();
	}
});